package exp2;

import ch16.ElementNotFoundException;
import ch16.LinkedBinaryTree;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Created by 春旺 on 2017/10/23.
 */
public class LinkedBinaryTreeTest {
    LinkedBinaryTree tree;

    @Test
    public void size() throws Exception {
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
       tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals(3,tree.size());
    }

    @Test
    public void getLeft() throws Exception {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals(2,tree.getLeft().getRootElement());

    }

    @Test
    public void getRight() throws Exception {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals(3,tree.getRight().getRootElement());
    }

    @Test
    public void contains() throws Exception, ElementNotFoundException {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals(false,tree.contains(4));
        assertEquals(true,tree.contains(2));
    }

    @Test
    public void isEmpty() throws Exception {
        LinkedBinaryTree tree1 = new LinkedBinaryTree();
        assertEquals(true,tree1.isEmpty());
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree1 = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals(false,tree1.isEmpty());
    }

    @Test
    public void postorder() throws Exception {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
       assertEquals("[2, 3, 1]",tree.postorder().toString());
    }

    @Test
    public void preorder() throws Exception {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals("[1, 2, 3]",tree.preorder().toString());
    }

    @Test
    public void testToString() throws Exception {
        LinkedBinaryTree treeL = new LinkedBinaryTree(2);
        LinkedBinaryTree treeR = new LinkedBinaryTree(3);
        tree = new LinkedBinaryTree(1,treeL,treeR);
        assertEquals("[1, 2, 3]",tree.toString());
    }

}